﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bll;
using Model;
using System.Data;

namespace Accounting.admin.Service
{
    public partial class ServiceAdd : System.Web.UI.Page
    {
        BllService b = new BllService();
        ModelPublic m = new ModelPublic();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                if (Session["Name"] != null && Session["Type"] != null)
                {
                    txtAuthor.Text = Session["Name"].ToString();

                    dataBind();
                    UserType.Text = Session["Type"].ToString();
                    txtDateTime.Text = DateTime.Now.ToString("yyyy-MM-dd");

                    if (UserType.Text == "0")
                    {
                        Release.Visible = true;
                    }
                    else
                    {
                        Release.Visible = false;
                    }
                }
                else
                {
                    btnRest.Enabled = false;
                    ASubmit.Disabled = false;
                }
            }
        }

        public void dataBind()
        {
            DataTable nav = b.Nav_Inquire_All();

            foreach (DataRow my in nav.Rows)
            {
                if (my["Id"].ToString() == "17")
                {
                    H2_Title.InnerText = "添加：" + my["Name"].ToString();
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            m.Title = "'" + txtTitle.Text.Trim() + "'";
            m.DateTime = "'" + txtDateTime.Text.Trim() + "'";
            m.Author = "'"+ txtAuthor.Text.Trim()+ "'";

            if (Content.Text == "")
                m.Content = "NULL";
            else
                m.Content ="'"+ Content.Text + "'";

            m.Number = txtNumber.Text.Trim();
            m.NavId = "17";
            

            if (ServiceAdress.Checked == true)
            {
                m.Adress = "'" + txtAdress.Text.Trim() + "'";
                m.Type = "0";
            }
                
            if (ServiceContent.Checked == true)
            {
                m.Type = "1";
                m.Adress = "NULL";
            }
                
            

            if (UserType.Text == "0")
            {
                m.Remarks = "NULL";
                if (Release.Checked == true)
                    m.Release = "'True'";
                else
                    m.Release = "'False'";
            }
            else
            {
                m.Remarks = "'stu'";
                m.Release = "'false'";
            }

            if (ReleaseFile.HasFile)
            {
                string Upname = ReleaseFile.FileName;

                int Pointposition = 0;
                string FileName = "";
                string suffix = "";

                for (int i = 0; i < Upname.Length; i++)
                {
                    if (Upname[i] == '.')
                    {
                        Pointposition = i;
                        break;
                    }
                }

                for (int i = 0; i < Pointposition; i++)
                {
                    FileName += Upname[i];
                }

                for (int i = Pointposition; i < Upname.Length; i++)
                {
                    suffix += Upname[i];
                }

                m.DateTime = txtDateTime.Text.Trim();

                int num = b.Service_Count_File_Number(m);

                string Time = System.DateTime.Now.ToString("yyyyMMdd");
                string folderRoute = "InformationFile/" + Time + (num + 1).ToString() + suffix;

                m.FileName = ReleaseFile.FileName;
                m.FileRoute = folderRoute;
            }
            else
            {
                m.FileName = "NULL";
                m.FileRoute = "NULL";
            }




            string message = b.Service_Add(m);
            if (message == "1")
            {
                Response.Write("<script>alert('添加成功！')</script>");
                
                //文件上传
                try
                {
                    ReleaseFile.SaveAs(Server.MapPath("../../" + m.FileRoute));
                }
                catch (Exception ex)
                {
                    Response.Write("<script>alert('上传失败:" + ex.Message + "')</script> ");
                }
                
            }
            else
            {
                Response.Write("<script>alert('" + message + "')</script>");
            }

        }

        private void ContentVerification(string message)
        {
            Response.Write("<script>alert('" + message + "')</script>");
        }

        protected void btnRest_Click(object sender, EventArgs e)
        {
            ClearContext();
        }

        private void ClearContext()
        {
            txtAuthor.Text = "";
            txtDateTime.Text = "";
            txtNumber.Text = "";
            txtTitle.Text = "";
            Content.Text = "";
        }
    }
}